﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Sync.File;
using Sync.Log;
using Sync.User;

namespace LoginUser.PopUpWindows
{
    /// <summary>
    /// Interaction logic for FileSyncChooser.xaml
    /// </summary>
    public partial class FileSyncChooser : Window
    {
        public string Path { get; set; }
        private FolderBrowserDialog browse;
        
        public FileSyncChooser()
        {
            Preferences.UseFile(@"..\..\", "Setup.ini");
            try
            {
                Preferences.IniConSource.AddConfig("Sync");
            }
            catch (Exception)
            {
                FileLog.LogMessage("[Sync] Already Added in Sync.ini",LogType.Warning);
            }
            if(!Preferences.IniConSource.Configs["Sync"].Contains("Destination1"))
                Preferences.IniConSource.Configs["Sync"].Set("Destination1", "");
            if (!Preferences.IniConSource.Configs["Sync"].Contains("Destination2"))
                Preferences.IniConSource.Configs["Sync"].Set("Destination2", "");
            
            InitializeComponent();
            browse = new FolderBrowserDialog();
            ResizeMode = ResizeMode.CanMinimize;
        }

        private void OriginBrowseButton_Click(object sender, RoutedEventArgs e)
        {
            if(browse.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Path = browse.SelectedPath;
                OriginTextBox.Text = Path;
                Preferences.IniConSource.Configs["Sync"].Set("Destination1", Path);
                Preferences.IniConSource.Save(Preferences.FullFilePath);
            }
        }

        private void DestinationBrowseButton_Click(object sender, RoutedEventArgs e)
        {
            if (browse.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Path = browse.SelectedPath;
                DestinationTextBox.Text = Path;
                Preferences.IniConSource.Configs["Sync"].Set("Destination2", Path);
                Preferences.IniConSource.Save(Preferences.FullFilePath);
            }
        }
        
        private void SyncButton_Click(object sender, RoutedEventArgs e)
        {
            new FileChecker(OriginTextBox.Text, DestinationTextBox.Text);
            Visibility = Visibility.Hidden;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (!String.IsNullOrEmpty(Preferences.IniConSource.Configs["Sync"].GetString("Destination1")))
            {
                OriginTextBox.Text = Preferences.IniConSource.Configs["Sync"].GetString("Destination1");
            }
            if (!String.IsNullOrEmpty(Preferences.IniConSource.Configs["Sync"].GetString("Destination2")))
            {
                DestinationTextBox.Text = Preferences.IniConSource.Configs["Sync"].GetString("Destination2");
            }
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            Visibility = Visibility.Hidden;
            MainWindow.Utilities.ShowDialog();
        }
    }
}
